#!/usr/bin/env python
# -*- coding:utf-8 -*-

from jujub import Jujube, Consumer, Producer

def DEBUG(format, *args) : print format % args

#######################################################################


class SimpleConsumer(Consumer)  :
    '''Echo Consumer'''
    def feed(self, packet, channel_from, dispatcher) :
        record = packet.body
        DEBUG('CONSUMER packet: %s', str(packet))
        DEBUG('CONSUMER echo: %s', record)
        return True
        # feed()
    # class SimpleConsumer

class   SimpleProducer(Producer) :
    def __init__(self, maximum) :
        self.maximum = maximum
        self.count = 0
        # __init__()

    def empty(self) :
        return  self.count >= self.maximum
        # empty()

    def more(self) :
        if self.empty() : return None
        self.count += 1
        msg = '#' + str(self.count) + ' hello, world.'
        return msg
        # more()
    # class SimpleProducer()


HOST = "127.0.0.1"
PORT = 54344
SERVICE = 'ECHO'

def main():
    producer = SimpleProducer(10)
    consumer = SimpleConsumer()

    address = (HOST, PORT)
    c = Jujube()
    c.connect(address)
    c.registerProducer(address, SERVICE, producer, consumer)

    c.runsync()

if __name__ == '__main__' :
    main()
    import sys
    sys.exit(0)
